<template>
    <div
        class="fixed top-0 bottom-0 left-0 right-0 flex flex-col justify-center login-page bg-gradient-to-br from-red-600 to-red-900">

        <div class="w-full px-5 pt-10 h-2/5">
            <div class="w-full h-full bg-bottom bg-no-repeat bg-contain bg-img"></div>
        </div>
        <div class="flex justify-center flex-1 pt-10">

            <div class="w-5/6 text-white max-w-96">
                <el-form :model="form">
                    <el-form-item>
                        <el-input v-model="form.loginName" size="large" maxlength="32" :prefix-icon="User"
                            placeholder="登录名" />
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="form.password" size="large" :show-password="true" maxlength="32"
                            :prefix-icon="Lock" placeholder="密码" />
                    </el-form-item>
                    <el-form-item>
                        <el-button :loading="loading" size="large" class="w-full" @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>

        </div>

        <el-dialog v-model="isShowSetPassword" title="设置新密码" width="90%" class="max-w-96" :close-on-click-modal="false"
            :close-on-press-escape="false">
            <el-form :model="formSetPassword">
                <el-form-item>
                    <el-input v-model="formSetPassword.password" :show-password="true" placeholder="请输入新密码" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="formSetPassword.passwordAgain" :show-password="true" placeholder="请确认新密码" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="isShowSetPassword = false">取消</el-button>
                    <el-button @click="doSetPassword" type="primary"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>

    </div>
</template>
  
<script setup>
import { ref, reactive, onMounted } from 'vue';
import useAuth from '@/stores/auth.js';
import { useRouter } from 'vue-router';
import { staff } from '@/common/getData';
import { User, Lock } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
const router = useRouter();
const loading = ref(false)
const form = reactive({
    loginName: '',
    password: ''
});

let token = '';
const isShowSetPassword = ref(false),
    formSetPassword = reactive({
        password: '',
        passwordAgain: ''
    });

const doSetPassword = async () => {
    let { password, passwordAgain } = formSetPassword;
    password = password.trim();
    passwordAgain = passwordAgain.trim();

    if (!password) {
        ElMessage({
            message: '请输入新密码',
            grouping: true,
            type: 'error',
        })
        return;
    }
    if (password !== passwordAgain) {
        ElMessage({
            message: '两次输入不一致',
            grouping: true,
            type: 'error',
        })
        return;
    }

    const callSetPassword = await staff('setPassword', { token, password });
    console.log(callSetPassword)
    const { state, msg } = callSetPassword;
    ElMessage({
        message: msg,
        grouping: true,
        type: state === 'setSuccess' ? 'success' : 'error',
    })

    if (state === 'setSuccess') isShowSetPassword.value = false;
}

const login = async () => {
    loading.value = true;
    let { loginName, password } = form;
    loginName = loginName.trim();
    password = password.trim();

    if (!loginName) {
        ElMessage({
            message: '请输入登录名',
            grouping: true,
            type: 'error',
        })
        return;
    }

    const callLogin = await staff('login', { loginName, password });
    console.log(callLogin);
    const { state, msg } = callLogin;
    ElMessage({
        message: msg,
        grouping: true,
        type: ['pass', 'setPassword'].includes(state) ? 'success' : 'error',
    })

    if (state === 'pass') {
        const auth = useAuth();
        auth.login(callLogin.token);
        router.push({ name: 'home' });
    }

    if (state === 'setPassword') {
        token = callLogin.token;
        isShowSetPassword.value = true;
    }

    loading.value = false;
}

onMounted(async () => {

});
</script>

<style>
.login-page .bg-img {
    background-image: url(@/assets/img/2025.png);
}

.login-page .el-input__wrapper {
    background-color: transparent !important;
}

.login-page .text-white .el-input__wrapper input {
    color: white !important;
}
</style>
